<?php


namespace app\api\controller;


use app\common\model\SchoolSchoolArea;
use app\common\model\SchoolSchoolImages;
use app\common\model\SchoolSchoolIntro;
use app\common\model\SchoolSchoolResource;
use app\common\model\SchoolSchoolViews;

class School
{
    /**
     * 学校展示首页
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function schoolIndex()
    {
        // 学校简介
        $intro = SchoolSchoolIntro::where('id', 1)->value('school_info');
        $school = SchoolSchoolArea::where('is_open', 1)->select();
        // 学校相册
        $views = SchoolSchoolViews::select();
        foreach ($views as $key => $value) {
            $images = SchoolSchoolImages::where('view_id', $value['id'])->select();
            $count = count($images);
            if ($count > 0) {
                $views[$key]['count'] = $count;
                $views[$key]['images'] = $images[0];
            }  else {
                unset($views[$key]);
            }
        }
        //活动资源
        $resource = SchoolSchoolResource::order('id desc')->select();
        resReturn([
            'intro' => $intro,
            'school' => $school,
            'views' => $views,
            'resource'=>$resource
        ]);
    }
    /**
     * 所有资源
     */
    public function allResource()
    {
        $resource = SchoolSchoolResource::order('id desc')->select();
        resReturn($resource);
    }

    /**
     * 环境所有图片
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function viewImage()
    {
        $view_id = input('param.view_id');
        $images = SchoolSchoolImages::where('view_id', $view_id)->select();
        resReturn($images);
    }
}